//
//  ProductDetailsViewController.m
//  SaltLightDemo
//
//  Created by lzy on 2018/5/9.
//  Copyright © 2018年 lzy. All rights reserved.
//

#import "ProductDetailsViewController.h"
#import "ApplicationCompletionViewController.h"
#import "Tool.h"
#import "MGRequestService+My.h"
#import "SingInViewController.h"
#import "PersonalSituationViewController.h"
// iPhone X 宏定义
#define  iPhoneX (SCREEN_WIDTH == 375.f && SCREEN_HEIGHT == 812.f ? YES : NO)
// 适配iPhone X 状态栏高度
#define  MC_StatusBarHeight      (iPhoneX ? -40.f : 0.f)
@interface ProductDetailsViewController ()
{
    float h;
    float view2_h;
    float view3_h;
}
@property (nonatomic , strong) UIScrollView *scrollview;
@property (nonatomic , strong) UIView *sview;

@property (nonatomic , strong) NSString *name;
@property (nonatomic , strong) NSString *amountMin;
@property (nonatomic , strong) NSString *amountMax;
@property (nonatomic , strong) NSString *termMin;
@property (nonatomic , strong) NSString *termMax;
@property (nonatomic , strong) NSString *termType;
@property (nonatomic , strong) NSString *monthRateMin;
@property (nonatomic , strong) NSString *monthRateMax;
@property (nonatomic , strong) NSString *repayment;


@property (nonatomic , strong) NSString *str1;
@property (nonatomic , strong) NSString *str2;
@end

@implementation ProductDetailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"产品详情";
    [MGRequestService requestgetProductInfo:self.type success:^(MGBaseRspModel *model) {
        if (model.resultCode == 0) {
            self.name=model.respData[@"name"];
            self.amountMin=model.respData[@"amountMin"];
            self.amountMax=model.respData[@"amountMax"];
//            self.termMin=([NSString stringWithFormat:@"%@",model.respData[@"termMin"]].length<=0||NULLString(model.respData[@"termMin"]))?@"0":model.respData[@"termMin"];
            self.termMin=model.respData[@"termMin"];
            self.termMax=model.respData[@"termMax"];
            self.termType=model.respData[@"termType"];
            self.monthRateMin=model.respData[@"monthRateMin"];
            self.monthRateMax=model.respData[@"monthRateMax"];
            self.repayment=model.respData[@"repayment"];
            self.str1 = model.respData[@"condition"];
            self.str2=model.respData[@"data"];
            [self updateInfo];
        }else
        {
            [self showToastInViewController:model.errorMessage];
        }
    } failure:^(NSError *error) {
        [self showToastInViewController:@"网络出错"];
    }];
    
    
    
    // Do any additional setup after loading the view.
}
-(void)updateInfo{
    
    view2_h = [Tool heightForLabelWithStr:self.str1 width:SCREEN_WIDTH-40 fontSize:14]+65;
    view3_h = [Tool heightForLabelWithStr:self.str1 width:SCREEN_WIDTH-40 fontSize:14]+65;
    h = SCREEN_WIDTH*(327/750.0) + 155 + view2_h + view3_h + 40 ;
    self.scrollview = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-64-60)];
    
    [self.view addSubview:self.scrollview];
    
    UIView * sview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, h)];
    self.sview = sview;
    self.sview.backgroundColor = [UIColor colorWithRed:237/255.0 green:237/255.0 blue:237/255.0 alpha:1];
    [self.scrollview addSubview:sview];
    self.scrollview.contentSize = CGSizeMake(SCREEN_WIDTH, h);
    
    UIButton * button = [[UIButton alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.scrollview.frame)+MC_StatusBarHeight, SCREEN_WIDTH, 60)];
    button.backgroundColor = [UIColor add_colorWithRGBHexString:@"FFD600"];
    [button setTitle:@"马上申请贷款" forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:14.0];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:button];
    
    [self addsubview];
}
-(void)back:(UIButton *)btn{
    if (!UserInfoManage.userInfo.token) {
        [self.navigationController pushViewController:[[SingInViewController alloc]init] animated:YES];
        return;
    }else if (!UserInfoManage.userInfo.nickName || (UserInfoManage.userInfo.nickName&&UserInfoManage.userInfo.nickName.length<=0)){
        [self.navigationController pushViewController:[[PersonalSituationViewController alloc]init] animated:YES];
        return;
    }else{
        [MGRequestService requestaddApplyOrder:self.type success:^(MGBaseRspModel *model) {
            if (model.resultCode == 0) {
                ApplicationCompletionViewController * ctr = [[ApplicationCompletionViewController alloc]initWithSelfClassNib];
                ctr.type = self.type;
                [self.navigationController pushViewController:ctr animated:YES];
            }else
            {
                [self showToastInViewController:model.errorMessage];
            }
            
        } failure:^(NSError *error) {
            [self showToastInViewController:@"网络出错"];
        }];
    }
    
}
-(void)addsubview
{
    //第一部分
    UIImageView * image = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_WIDTH*(327/750.0))];
    image.image = [UIImage imageNamed:@"dkxq_icon_img_bj"];
    [self.sview addSubview:image];
    
    UILabel * label11 = [[UILabel alloc]initWithFrame:CGRectMake(0, SCREEN_WIDTH*(327/750.0)*(114/327.0), SCREEN_WIDTH, 30)];
    label11.font = [UIFont boldSystemFontOfSize:18.0];
    label11.text = self.name;
    label11.textColor = [UIColor whiteColor];
    label11.textAlignment = NSTextAlignmentCenter;
    [image addSubview:label11];
    
//    UILabel * label12 = [[UILabel alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(label11.frame), SCREEN_WIDTH, 20)];
//    label12.font = [UIFont boldSystemFontOfSize:14.0];
//    label12.text = @"还款期限长，不考察，先息后本";
//    label12.textColor = [UIColor whiteColor];
//    label12.textAlignment = NSTextAlignmentCenter;
//    [image addSubview:label12];
    
    
    UIView * view1 = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(image.frame), SCREEN_WIDTH, 155)];
    view1.backgroundColor = [UIColor whiteColor];
    [self.sview addSubview:view1];
    
    UIImageView * image1 = [[UIImageView alloc]initWithFrame:CGRectMake((SCREEN_WIDTH/3-35)/2, 20, 35, 35)];
    image1.image = [UIImage imageNamed:@"最高额度"];
    [view1 addSubview:image1];
    
    
    UIImageView * image2 = [[UIImageView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/3+(SCREEN_WIDTH/3-35)/2, 20, 35, 35)];
    image2.image = [UIImage imageNamed:@"期限"];
    [view1 addSubview:image2];
    
    UIImageView * image3 = [[UIImageView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH*2/3+(SCREEN_WIDTH/3-35)/2, 20, 35, 35)];
    image3.image = [UIImage imageNamed:@"月息"];
    [view1 addSubview:image3];
    
    for (int i = 0; i<3; i++) {
        UILabel * label1 = [[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/3*i, CGRectGetMaxY(image2.frame)+5, SCREEN_WIDTH/3, 20)];
        NSArray * arr = @[@"最高额度",@"期限",@"月息"];
        label1.font = [UIFont boldSystemFontOfSize:14.0];
        label1.text = arr[i];
        label1.textColor = [UIColor add_colorWithRGBHexString:@"CCCCCC"];
        label1.textAlignment = NSTextAlignmentCenter;
        [view1 addSubview:label1];
        
        
        UILabel * label2 = [[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/3*i, CGRectGetMaxY(label1.frame)+5, SCREEN_WIDTH/3, 20)];
        NSArray * arr2 = @[[NSString stringWithFormat:@"%d-%d万",[self.amountMin intValue]/10000,[self.amountMax intValue]/10000],[self.termType intValue] == 1?[NSString stringWithFormat:@"%@-%@月",self.termMin,self.termMax]:[NSString stringWithFormat:@"%@-%@年",self.termMin,self.termMax],(self.monthRateMin<=0||self.monthRateMax<=0)?@"面议":[NSString stringWithFormat:@"%@-%@%@",self.monthRateMin,self.monthRateMax,@"%"]];
        label2.font = [UIFont boldSystemFontOfSize:16.0];
        label2.text = arr2[i];
        label2.textColor = [UIColor add_colorWithRGBHexString:@"333333"];
        label2.textAlignment = NSTextAlignmentCenter;
        [view1 addSubview:label2];
    }
    UIView * dian = [[UIView alloc]initWithFrame:CGRectMake(35, CGRectGetMaxY(image2.frame)+50+(155-(CGRectGetMaxY(image2.frame)+50)-10)/2.0, 10, 10)];
    dian.backgroundColor = [UIColor add_colorWithRGBHexString:@"FFD600"];
    dian.layer.masksToBounds = YES;
    dian.layer.cornerRadius = 5.0;
    [view1 addSubview:dian];
    
    UILabel * label3 = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(dian.frame)+5, CGRectGetMaxY(image2.frame)+50, SCREEN_WIDTH-(CGRectGetMaxX(dian.frame)+5)-20, 155-(CGRectGetMaxY(image2.frame)+50))];
    label3.font = [UIFont boldSystemFontOfSize:14.0];
    label3.text = [NSString stringWithFormat:@"还款方式：%@",self.repayment];
    label3.textColor = [UIColor add_colorWithRGBHexString:@"333333"];
    label3.textAlignment = NSTextAlignmentLeft;
    [view1 addSubview:label3];
    
    //第二部分
    UIView * view2 = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(view1.frame)+10, SCREEN_WIDTH, view2_h)];
    view2.backgroundColor = [UIColor whiteColor];
    [self.sview addSubview:view2];
    
    UIImageView * icon = [[UIImageView alloc]initWithFrame:CGRectMake(15, 15, 20, 20)];
    icon.image = [UIImage imageNamed:@"dkxq_icon_bq"];
    [view2 addSubview:icon];
    UILabel * label21 = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(icon.frame)+5, 15, 100, 20)];
    label21.font = [UIFont boldSystemFontOfSize:14.0];
    label21.text = @"申请条件";
    label21.textColor = [UIColor add_colorWithRGBHexString:@"999999"];
    label21.textAlignment = NSTextAlignmentLeft;
    [view2 addSubview:label21];
    
    UILabel * label22 = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(icon.frame)+5, CGRectGetMaxY(icon.frame)+15, SCREEN_WIDTH-(CGRectGetMaxX(icon.frame)+5)-30,[Tool heightForLabelWithStr:self.str1 width:SCREEN_WIDTH-40 fontSize:14])];
    label22.font = [UIFont boldSystemFontOfSize:14.0];
    label22.text = self.str1;
    label22.numberOfLines = 0;
    label22.textColor = [UIColor add_colorWithRGBHexString:@"333333"];
    label22.textAlignment = NSTextAlignmentLeft;
    [view2 addSubview:label22];
    
    //第三部分
    
    UIView * view3 = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(view2.frame)+10, SCREEN_WIDTH, view3_h)];
    view3.backgroundColor = [UIColor whiteColor];
    [self.sview addSubview:view3];
    
    UIImageView * icon2 = [[UIImageView alloc]initWithFrame:CGRectMake(15, 15, 20, 20)];
    icon2.image = [UIImage imageNamed:@"dkxq_icon_bq"];
    [view3 addSubview:icon2];
    UILabel * label31 = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(icon.frame)+5, 15, 100, 20)];
    label31.font = [UIFont boldSystemFontOfSize:14.0];
    label31.text = @"所需资料";
    
    label31.textColor = [UIColor add_colorWithRGBHexString:@"999999"];
    label31.textAlignment = NSTextAlignmentLeft;
    [view3 addSubview:label31];
    
    UILabel * label32 = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(icon2.frame)+5, CGRectGetMaxY(icon2.frame)+15, SCREEN_WIDTH-(CGRectGetMaxX(icon2.frame)+5)-30,[Tool heightForLabelWithStr:self.str2 width:SCREEN_WIDTH-40 fontSize:14])];
    label32.font = [UIFont boldSystemFontOfSize:14.0];
    label32.text = self.str2;
    label32.numberOfLines = 0;
    label32.textColor = [UIColor add_colorWithRGBHexString:@"333333"];
    label32.textAlignment = NSTextAlignmentLeft;
    [view3 addSubview:label32];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
